package cn.gaily.edustatistics.controller;


import cn.gaily.commonutils.R;
import cn.gaily.edustatistics.entity.StatisticsDaily;
import cn.gaily.edustatistics.service.StatisticsDailyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-08-22
 */
@Api(description = "统计接口")
@RestController
@RequestMapping("/edustatistics/statistics-daily")
@CrossOrigin
public class StatisticsDailyController {
    @Autowired
    private StatisticsDailyService statisticsDailyService;

    @ApiOperation(value = "统计某天注册人数并存入数据库")
    @PostMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count =  statisticsDailyService.countRegister(day);
        StatisticsDaily entity = new StatisticsDaily();
        entity.setDateCalculated(day);
        entity.setRegisterNum(count);
        entity.setVideoViewNum(RandomUtils.nextInt(100,200));
        entity.setLoginNum(RandomUtils.nextInt(100,200));
        entity.setCourseNum(RandomUtils.nextInt(100,200));

        statisticsDailyService.save(entity);
        return R.ok();
    }

    @ApiOperation(value = "取统计数据")
    @PostMapping("showData/{type}/{begin}/{end}")
    public R showData(@PathVariable String type,
                      @PathVariable String begin,
                      @PathVariable String end){
        Map<String,Object> map = statisticsDailyService.showData(type,begin,end);
        return R.ok().data(map);
    }
}

